package org.jeecg.modules.hc.utils;


import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.TreeMap;

/**
 * author作者：lilingjie
 */
@Component
public class ALiSMSUtils {

    private static String accessKeyId = "LTAI5t73gzGH975QGrhBpUqK";

    private static String accessKeySecret = "Kyt6Pc6UN3IfBxkXgC6R2X0OsB1dLh";

    private static String templateCode = "SMS_461880537";//售后通知短信模板CODE

    /**
     * 发送短信
     *
     * @param phone        接收短信的手机号
     * @return
     */
    public Boolean sendMessage(String phone, String content) {

        Map<String, Object> codeMap = new TreeMap<>();
        codeMap.put("name",content);

        /**
         * 连接阿里云：
         *
         * 三个参数：
         * regionId 不要动，默认使用官方的
         * accessKeyId 自己的用户accessKeyId
         * accessSecret 自己的用户accessSecret
         */
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        // 构建请求：
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");

        // 自定义参数：
        request.putQueryParameter("PhoneNumbers", phone);// 手机号
        request.putQueryParameter("SignName", "用膳团");// 短信签名
        request.putQueryParameter("TemplateCode", templateCode);// 短信模版CODE

        // 构建短信
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(codeMap));

        try {
            CommonResponse response  = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

//    public static void main(String[] args) {
//        List<String> phoneList = new ArrayList<>();
//        phoneList.add("18627360735");
//        phoneList.add("19994641220");
//        phoneList.add("18826802770");
//        phoneList.forEach( p -> {
//            sendMessage(p,"f公司");
//        });
//    }
}
